package org.tgit.gateway.common;

import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
* @ClassName: MD5Utils   
* @Description: TODO(请求URI加密工具)   
* @author fanzaijun 
* @date 2018年8月23日 下午6:10:33   
*
 */
public class MD5Utils {
	private static final Logger LOGGER = LoggerFactory.getLogger(MD5Utils.class);

	private MD5Utils(){
		// Do nothing
	}
	private static char[] hexDigits = {
		'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
	};
	
	public static String md5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] bs = str.getBytes("UTF-8");
			md.reset();
			md.update(bs);
			byte[] md5bytes = md.digest();
			return md5byte2Str(md5bytes);
		}catch(Exception e) {
			LOGGER.error("传入的字符串无法进行md5加密", e);
			return "";
		}
	}
	
	private static String md5byte2Str(byte[] md5bytes) {
		char[] md5chars = new char[32];
		int k = 0;
		for(byte b : md5bytes) {
			md5chars[k++] = hexDigits[b >>> 4 & 0xf];
			md5chars[k++] = hexDigits[b & 0xf];
		}
		return new String(md5chars);
	}
}
